package com.zzr.demo.redis.controller;

import com.zzr.demo.redis.service.CounterService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author: xiaomi
 * @date: 2021/10/8
 * @description:
 */
@RestController
@RequestMapping("unique/counter")
@Api(tags = "唯一计数器：对不同元素进行计数")
@Slf4j
public class UniqueCounterController {

    @Autowired
    @Qualifier("UniqueCounter")
    CounterService counterService;

    @Autowired
    @Qualifier("UniqueCounterByHyperLogLog")
    CounterService hllCounterService;

    @GetMapping("/increase/{id}")
    @ApiOperation("增加计数")
    public boolean increase(@PathVariable("id") String id) {
        CounterService.iptl.set(id);
        counterService.increase();
        return true;
    }


    @GetMapping("/increaseByHLL/{id}")
    @ApiOperation("增加计数")
    public boolean increaseByHLL(@PathVariable("id") String id) {
        CounterService.iptl.set(id);
        long increase = hllCounterService.increase();
        log.info("数量为：{}", increase);
        return true;
    }

}
